package so.shanku.essential.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import aym.util.getservicesdata.GetServicesDataQueue;
import aym.util.getservicesdata.GetServicesDataUtil;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseActivity;
import so.shanku.essential.getdata.GetDataConfing;
import so.shanku.essential.getdata.GetDataQueue;
import so.shanku.essential.getdata.ShowGetDataError;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.StringUtil;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2015/9/26.
 */
public class ForgetPasswordSecondActivity extends BaseActivity {
    @ViewInject(id=R.id.fill_view,click = "fill_view_click")
    private LinearLayout fill_view;
	/**
	 * 验证码
	 */
	@ViewInject(id=R.id.et_input_code)
	private EditText et_input_code;
	/**
	 * 输入密码
	 */
	@ViewInject(id=R.id.et_input_password)
	private EditText et_input_password;
	/**
	 * 确认密码
	 */
	@ViewInject(id=R.id.et_confirm_password)
	private EditText et_confirm_password;
	/**
	 * 确定按钮
	 * @param savedInstanceState
	 */
	@ViewInject(id=R.id.et_confirm,click = "et_confirm_click")
	private TextView et_confirm;
	private String input_code; //获取验证码输入框中验证码
	private String input_password; //获取输入密码输入框中密码
	private String input_confirm_password; //获取输入密码输入框中确认密码

	private String phone_or_email;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_forget_password_second);
		phone_or_email=getIntent().getStringExtra(ExtraKeys.Key_Msg1);
	}

    /**
     * 空白处 结束
     * @param view
     */
    public void fill_view_click(View view){
        finish();
    }

	public void et_confirm_click(View view){
		input_code=et_input_code.getText().toString();
		input_password=et_input_password.getText().toString();
		input_confirm_password=et_confirm_password.getText().toString();
		if(TextUtils.isEmpty(input_code)){
			toast.showToast(R.string.pls_input_code);
			return;
		}
		if(!isPasswordAuthentication(input_password)){
			return;
		}
		if(!input_confirm_password.equals(input_password)){
			toast.showToast(R.string.password_differ);
			return;
		}
		getData_Forget_Password();

	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			setResult(RESULT_OK);
			finish();
		}
	}
	/**
	 * 密码判断
	 */
	public boolean isPasswordAuthentication(String password) {
		if(!StringUtil.passwordAuthenticationLength(password)){
			toast.showToast(R.string.register_passwordRule);
			return false;
		} else if (!StringUtil.passwordAuthentication(password)){
			toast.showToast(R.string.password_format_error);
			return false;
		}
		return true;
	}
	public void getData_Forget_Password(){

		loadingToast.show();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(GetDataQueue.Params_key, GetDataConfing.Key_Str);

		params.put("userName", phone_or_email);
		params.put("password",input_password);
		params.put("code",input_code);

		GetDataQueue queue = new GetDataQueue();
		queue.setActionName(GetDataConfing.Action_GetSendPhoneTwo);
		queue.setWhat(GetDataConfing.What_GetSendPhoneTwo);
		queue.setParamsNoJson(params);
		queue.setCallBack(callBack);
		getDataUtil.getData(queue);

	}
	private GetServicesDataUtil.IGetServicesDataCallBack callBack = new GetServicesDataUtil.IGetServicesDataCallBack() {
		@Override
		public void onLoaded(GetServicesDataQueue entity) {
			if (entity.isOk()) {
				if (ShowGetDataError.isOkAndCodeIsNot1(mContext, entity.getInfo())) {
					Intent intent=new Intent(mContext,ForgetPasswordThirdActivity.class);
					intent.putExtra(ExtraKeys.Key_Msg1, phone_or_email);
					startActivityForResult(intent, 1);
				}
			} else {
				ShowGetDataError.showNetError(mContext);
			}
			loadingToast.dismiss();
		}
	};
}
